﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Media;
using System.Diagnostics;
using System.Threading;
using Generator;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class MainForm
    {
        private void Generate()
        {
            if (CurrentTemplate == null ||
                CurrentRota == null)
            {
                return;
            }

            #region if the template is empty - tell the user & quit
            if (CurrentTemplate.GetAllShifts.Count() == 0)
            {
                MessageBox.Show(
                    Resources.MESSAGEBOX_INFO_ShouldCreateTemplate_MESSAGE.Replace(
                    "#",
                    CurrentTemplate.Name),
                    Resources.MESSAGEBOX_INFO_ShouldCreateTemplate_CAPTION,                    
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;
            }
            #endregion

            #region if the rota is not empty - ask the user whether they're OK with overwriting it
            if (CurrentRota.GetAllShifts.Count() > 0)
            {
                var proceed =
                    MessageBox.Show(
                        Resources.MESSAGEBOX_QUESTION_OverwriteExistingRota_MESSAGE,
                        Resources.MESSAGEBOX_QUESTION_OverwriteExistingRota_CAPTION,
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question);
                if (proceed == DialogResult.No)
                {
                    return;
                }
            }
            #endregion

            Report.Started = DateTime.Now;
            Penalties.PreparePenaltyCharts();
            Program.generaterotaForm = new SetGeneratorParameters(
                CurrentRota.Monday.WeekOfYear(),
                CurrentTemplate.Name,
                true,
                lastUsed,
                Program.CurrentFile.PreviousWeekClosers(CurrentRota).Value);
            
            bool showReport; // false by default
            if (Program.generaterotaForm.ShowDialog() == DialogResult.OK)
            {
                lastUsed = Program.generaterotaForm.BoundParameters;
                showReport = Program.generaterotaForm.ShowReport;
            }
            else
            {
                return;
            }
                        
            Program.generatorForm = new GeneratingRota(
                svRota.refill as Rota,
                svTemplate.refill as Template,
                lastUsed);

            if (Program.generatorForm.ShowDialog() == DialogResult.Cancel)
            {
                if (Program.generatorForm.impossibleDays[7])
                {
                    CurrentRota.FinalReport = Report.GetFailedReport(Program.generatorForm.impossibleDays);
                    CurrentRota.FinalReport.Completed = DateTime.Now;
                    showReport = true; // if we failed, report will be shown even if user checked this option off
                }
                else
                {
                    return;
                }
            }
            svRota.TotalRefresh();
            
            if (!showReport)
            {
                return;
            }
            Program.reportForm = new GeneratorReport(CurrentRota.FinalReport);
            Program.reportForm.Show();
        }
    }
}
